Windows Desktop Duplication
This is meant to provide a low latency, low level access to desktop frames for use
in applications like Game streaming (e.g., Google Stadia, Microsoft XCloud).
Crate provides convenient wrapper for acquiring gpu
textures
from Windows desktop duplication api.
The crate includes some convenient features that the source api does not provide
Async Example
Although this example shows using TextureReader
, for best performance, you want to use the texture directly to
encode via one of the hardware based encoders like nvenc or quick-sync.
use win_desktop_duplication::*;
use win_desktop_duplication::{tex_reader::*, devices::*};
#[tokio::main(flavor = "current_thread")]
async fn main() {
set_process_dpi_awareness();
co_init();
let adapter = AdapterFactory::new().get_adapter_by_idx(0).unwrap();
let output = adapter.get_display_by_idx(0).unwrap();
let mut dupl = DesktopDuplicationApi::new(adapter, output).unwrap();
let (device, ctx) = dupl.get_device_and_ctx();
let mut texture_reader = TextureReader::new(device, ctx);
let mut pic_data: Vec<u8> = vec![0; 0];
loop {
let tex = dupl.acquire_next_vsync_frame().await;
if let Ok(tex) = tex {
texture_reader.get_data(&mut pic_data, &tex);
}
}
}
Sync Example
Although this example shows using TextureReader
, for best performance, you want to use the texture directly to
encode
via one of the hardware based encoders like nvenc or quick-sync.
use win_desktop_duplication::*;
use win_desktop_duplication::{tex_reader::*, devices::*};
fn main() {
set_process_dpi_awareness();
co_init();
let adapter = AdapterFactory::new().get_adapter_by_idx(0).unwrap();
let output = adapter.get_display_by_idx(0).unwrap();
let mut dupl = DesktopDuplicationApi::new(adapter, output.clone()).unwrap();
let (device, ctx) = dupl.get_device_and_ctx();
let mut texture_reader = TextureReader::new(device, ctx);
let mut pic_data: Vec<u8> = vec![0; 0];
loop {
output.wait_for_vsync().unwrap();
let tex = dupl.acquire_next_frame_now();
if let Ok(tex) = tex {
texture_reader.get_data(&mut pic_data, &tex);
}
}
}
Features